﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;
using System.Collections.Generic;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void CreateUser(string name, string email, string password, int attack, int defnese, int speed, int culture, int production, int science, int growth, bool startupBonus)
    {
        //get avg stats
        Dictionary<string, double> stats = UserDefinedFunctions.AvgSciences();

        //if (attack + defnese + speed + culture + production + science + growth != 0) return;
        int oId = 0, pId = 0;
        int lastOrginId = int.Parse(GetConfigurationForOption("LastOrginID"));
        int playersInSystem = int.Parse(GetConfigurationForOption("PlayersInLastSystem"));

        if (playersInSystem > 2)
        {
            oId = lastOrginId + 1;
            playersInSystem = 0;
        }
        else
            oId = lastOrginId;

        pId = 2 + 4 * playersInSystem;
        InsertNews(8, DateTime.Now, "Create player: lo: " + lastOrginId.ToString() + " pis:" + playersInSystem.ToString() + " oid: " + oId.ToString() + " pid: " + pId.ToString());
        //store configuration
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Update configuration set LastOrginID=@o, PlayersInLastSystem=@p", conn))
            {
                cmd.Parameters.Add("@o", SqlDbType.Int).Value = oId;
                cmd.Parameters.Add("@p", SqlDbType.Int).Value = playersInSystem + 1;
                cmd.ExecuteNonQuery();
            }
        }
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(@"SELECT     Count(*)
FROM         dbo.Players
WHERE     (PlayerName  = @PlayerName )
", conn))
            {
                cmd.Parameters.Add("@PlayerName", SqlDbType.NVarChar).Value = name;
                object c = cmd.ExecuteScalar();
                int count = int.Parse(c.ToString());
                if (count > 0) return;
            }
        }
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(
                "", conn))
            {
                //    cmd.CommandText = "Insert into Players(PlayerName,email, Password,Attack,Defense,Speed,Culture,Production,Growth,Science, OrginID, CurrentCulture, CurrentScience, ExpPoints, ArtifactID, Biology, Economy, Energy, Math, Physics, Social ) Values (@PlayerName,@email, @Password,@Attack,@Defense,@Speed,@Culture,@Production,@Growth,@Science, @oId, @cc, 1, 0, 0, @bio, @eco, @ene, @phy, @math, @soc)";
                //else
                cmd.CommandText = "Insert into Players(PlayerName,email, Password,Attack,Defense,Speed,Culture,Production,Growth,Science, OrginID, CurrentCulture, CurrentScience, ExpPoints, ArtifactID, Biology, Economy, Energy, Math, Physics, Social ) Values (@PlayerName,@email, @Password,@Attack,@Defense,@Speed,@Culture,@Production,@Growth,@Science, @oId, @cc, 1, 0, 0, @bio, @eco, @ene, @phy, @math, @soc)";

                cmd.Parameters.Add("@PlayerName", SqlDbType.NVarChar).Value = name;
                cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
                cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = StaticMethods.GetMd5Sum(name + password);
                cmd.Parameters.Add("@Attack", SqlDbType.Int).Value = attack;
                cmd.Parameters.Add("@Defense", SqlDbType.Int).Value = defnese;
                cmd.Parameters.Add("@Speed", SqlDbType.Int).Value = speed;
                cmd.Parameters.Add("@Culture", SqlDbType.Int).Value = culture;
                cmd.Parameters.Add("@Production", SqlDbType.Int).Value = production;
                cmd.Parameters.Add("@Growth", SqlDbType.Int).Value = growth;
                cmd.Parameters.Add("@Science", SqlDbType.Int).Value = science;
                cmd.Parameters.Add("@oId", SqlDbType.Int).Value = oId;
                if (stats["Culture"] > 0)
                    cmd.Parameters.Add("@cc", SqlDbType.Float).Value = stats["Culture"] * 0.85;
                else
                    cmd.Parameters.Add("@cc", SqlDbType.Float).Value = 1;

                cmd.Parameters.Add("@bio", SqlDbType.Float).Value = stats["Biology"] * 0.85;
                cmd.Parameters.Add("@eco", SqlDbType.Float).Value = stats["Economy"] * 0.85;
                cmd.Parameters.Add("@ene", SqlDbType.Float).Value = stats["Energy"] * 0.85;
                cmd.Parameters.Add("@phy", SqlDbType.Float).Value = stats["Physics"] * 0.85;
                cmd.Parameters.Add("@math", SqlDbType.Float).Value = stats["Math"] * 0.85;
                if (startupBonus == true)
                    cmd.Parameters.Add("@soc", SqlDbType.Float).Value = stats["Social"] * 0.85 + 2.0;
                else
                    cmd.Parameters.Add("@soc", SqlDbType.Float).Value = stats["Social"] * 0.85;

                cmd.ExecuteNonQuery();

                conn.Close();
            }
        }
        ColonizePlanet(oId, pId, UserDefinedFunctions.GetPlayerID(name).Value);
        if (startupBonus == true)
        {
            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(
                    "Update planets Set Labs = 10 Where PlanetId= @pId and SystemId = @sId", conn))
                {
                    cmd.Parameters.Add("@pId", SqlDbType.Int).Value = pId;
                    cmd.Parameters.Add("@sId", SqlDbType.Int).Value = oId;
                    cmd.ExecuteNonQuery();
                }
            }
        }
        TimeSpan ts = DateTime.Now - GetGameStart();

        AddProductionPoints(oId, pId, 10000 + (int)(ts.TotalHours * UserDefinedFunctions.GetAvgProduction() * 0.85));
    }
};
